package com.fr.report.js;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONUtils;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.cellElement.Formula;
import com.fr.report.parameter.Parameter;
import com.fr.web.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaScriptImpl extends AbstractJavaScript
{
  private List jsImportList = null;
  private String content;

  public JavaScriptImpl()
  {
  }

  public JavaScriptImpl(String paramString)
  {
    this.content = paramString;
  }

  public String createJS(Repository paramRepository)
  {
    Map localMap = paramRepository.checkoutTplContext();
    Parameter[] arrayOfParameter = getParameters();
    for (int i = 0; i < arrayOfParameter.length; ++i)
    {
      Object localObject = arrayOfParameter[i].getValue();
      if (localObject instanceof Formula)
        localObject = (((Formula)localObject).getResult() != null) ? ((Formula)localObject).getResult() : localObject;
      localMap.put(arrayOfParameter[i].getName(), localObject);
    }
    StringBuffer localStringBuffer = new StringBuffer();
    int j = 0;
    int k = getJSImportSize();
    while (j < k)
    {
      localStringBuffer.append("FR.$import(\"" + CodeUtils.javascriptEncode(getJSImport(j)) + "\"" + ", 'js'); ");
      ++j;
    }
    if (StringUtils.isNotBlank(this.content))
      localStringBuffer.append(createContentFunction());
    String str = localStringBuffer.toString();
    try
    {
      return TemplateUtils.render(str, localMap);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return str;
  }

  private String createContentFunction()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(";return (function(");
    Parameter[] arrayOfParameter = getParameters();
    for (int i = 0; i < arrayOfParameter.length; ++i)
    {
      if (i != 0)
        localStringBuffer.append(",");
      localStringBuffer.append(arrayOfParameter[i].getName());
    }
    localStringBuffer.append("){" + ((this.content == null) ? "" : this.content) + "})");
    localStringBuffer.append(".createDelegate(this, [");
    for (i = 0; i < arrayOfParameter.length; ++i)
    {
      if (i != 0)
        localStringBuffer.append(",");
      Object localObject = arrayOfParameter[i].getValue();
      if (localObject instanceof Formula)
        localObject = (((Formula)localObject).getResult() != null) ? ((Formula)localObject).getResult() : localObject;
      localObject = JSONUtils.quoteFrObject(localObject);
      if ((localObject != null) && ("".equals(localObject.toString())))
        localObject = null;
      localStringBuffer.append(localObject);
    }
    localStringBuffer.append("], 0).apply(this, arguments);");
    return localStringBuffer.toString();
  }

  public int getJSImportSize()
  {
    return ((this.jsImportList == null) ? 0 : this.jsImportList.size());
  }

  public String getJSImport(int paramInt)
  {
    return ((this.jsImportList == null) ? null : (String)this.jsImportList.get(paramInt));
  }

  public void addJSImort(String paramString)
  {
    if (this.jsImportList == null)
      this.jsImportList = new ArrayList();
    this.jsImportList.add(paramString);
  }

  public void clearJSImports()
  {
    if (this.jsImportList != null)
    {
      this.jsImportList.clear();
      this.jsImportList = null;
    }
  }

  public String getContent()
  {
    return this.content;
  }

  public void setContent(String paramString)
  {
    this.content = paramString;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isAttr())
      clearJSImports();
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Content"))
      {
        str2 = null;
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          setContent(str2);
      }
      else if (str1.equals("JSImport"))
      {
        str2 = null;
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          addJSImort(str2);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    int i = 0;
    int j = getJSImportSize();
    while (i < j)
    {
      paramXMLPrintWriter.startTAG("JSImport").textNode(getJSImport(i)).end();
      ++i;
    }
    paramXMLPrintWriter.startTAG("Content").textNode(getContent()).end();
  }

  public boolean equals(Object paramObject)
  {
    if (this == paramObject)
      return true;
    if ((!(paramObject instanceof JavaScriptImpl)) || (!(super.equals(paramObject))))
      return false;
    return ((ComparatorUtils.equals(getContent(), ((JavaScriptImpl)paramObject).getContent())) && (ComparatorUtils.equals(this.jsImportList, ((JavaScriptImpl)paramObject).jsImportList)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    JavaScriptImpl localJavaScriptImpl = (JavaScriptImpl)super.clone();
    if (this.jsImportList != null)
    {
      localJavaScriptImpl.jsImportList = new ArrayList();
      localJavaScriptImpl.jsImportList.addAll(this.jsImportList);
    }
    return localJavaScriptImpl;
  }
}